from app.core.config import settings
from app.services.ai_manager import ai_service_manager
from app.services.storage_manager import storage_manager  
from app.services.payment_manager import payment_manager
from app.services.ai_base import AIServiceProvider
from app.services.storage_base import StorageProvider
from app.services.payment_base import PaymentProvider

def initialize_ai_services():
    """初始化AI服务"""
    
    # 优先注册即梦AI服务（作为默认服务）
    if settings.JIMENG_AI_ENABLED:
        config = {
            "api_key": settings.JIMENG_AI_API_KEY,
            "user_id": settings.JIMENG_AI_USER_ID,
            "model_name": settings.JIMENG_AI_MODEL_NAME,
            "max_duration": settings.JIMENG_AI_MAX_DURATION,
        }
        
        ai_service_manager.register_service(
            AIServiceProvider.JIMENG_AI,
            config
        )
    
    # OpenAI服务已禁用 - 仅使用即梦AI
    # if settings.OPENAI_API_KEY:
    #     ai_service_manager.register_service(
    #         AIServiceProvider.OPENAI_DALLE,
    #         {
    #             "api_key": settings.OPENAI_API_KEY,
    #             "base_url": settings.OPENAI_BASE_URL
    #         }
    #     )
    
    # 注册Stability AI服务
    if settings.STABILITY_AI_API_KEY:
        ai_service_manager.register_service(
            AIServiceProvider.STABILITY_AI,
            {
                "api_key": settings.STABILITY_AI_API_KEY,
                "base_url": settings.STABILITY_AI_BASE_URL
            }
        )
    
    # 注册Google Veo 3服务
    if settings.GOOGLE_VEO3_API_KEY and settings.GOOGLE_VEO3_PROJECT_ID:
        config = {
            "api_key": settings.GOOGLE_VEO3_API_KEY,
            "project_id": settings.GOOGLE_VEO3_PROJECT_ID,
            "location": settings.GOOGLE_VEO3_LOCATION,
            "service_account_path": settings.GOOGLE_VEO3_SERVICE_ACCOUNT_PATH,
            "max_duration": settings.GOOGLE_VEO3_MAX_DURATION,
        }
        
        ai_service_manager.register_service(
            AIServiceProvider.GOOGLE_VEO3,
            config
        )
    
    # 即梦AI服务已在顶部优先注册
    # if settings.JIMENG_AI_ENABLED:
    #     config = {
    #         "api_key": settings.JIMENG_AI_API_KEY,
    #         "user_id": settings.JIMENG_AI_USER_ID,
    #         "model_name": settings.JIMENG_AI_MODEL_NAME,
    #         "max_duration": settings.JIMENG_AI_MAX_DURATION,
    #     }
    #     
    #     ai_service_manager.register_service(
    #         AIServiceProvider.JIMENG_AI,
    #         config
    #     )

def initialize_storage_services():
    """初始化存储服务"""
    
    # 注册MinIO服务
    # storage_manager.register_service(
    #     StorageProvider.MINIO,
    #     {
    #         "endpoint": settings.MINIO_ENDPOINT,
    #         "access_key": settings.MINIO_ACCESS_KEY,
    #         "secret_key": settings.MINIO_SECRET_KEY,
    #         "bucket_name": settings.MINIO_BUCKET_NAME,
    #         "secure": settings.MINIO_SECURE
    #     },
    #     is_default=True
    # )

def initialize_payment_services():
    """初始化支付服务"""
    
    # 注册微信支付
    if settings.WECHAT_PAY_APP_ID and settings.WECHAT_PAY_MCH_ID and settings.WECHAT_PAY_API_KEY:
        payment_manager.register_service(
            PaymentProvider.WECHAT_PAY,
            {
                "app_id": settings.WECHAT_PAY_APP_ID,
                "mch_id": settings.WECHAT_PAY_MCH_ID,
                "api_key": settings.WECHAT_PAY_API_KEY,
                "notify_url": settings.WECHAT_PAY_NOTIFY_URL,
                "cert_path": settings.WECHAT_PAY_CERT_PATH,
                "key_path": settings.WECHAT_PAY_KEY_PATH
            },
            is_default=True
        )
    
    # 注册支付宝
    if settings.ALIPAY_APP_ID and settings.ALIPAY_PRIVATE_KEY:
        payment_manager.register_service(
            PaymentProvider.ALIPAY,
            {
                "app_id": settings.ALIPAY_APP_ID,
                "private_key": settings.ALIPAY_PRIVATE_KEY,
                "alipay_public_key": settings.ALIPAY_PUBLIC_KEY,
                "notify_url": settings.ALIPAY_NOTIFY_URL,
                "return_url": settings.ALIPAY_RETURN_URL,
                "gateway_url": settings.ALIPAY_GATEWAY_URL
            }
        )

def initialize_all_services():
    """初始化所有外部服务"""
    initialize_ai_services()
    initialize_storage_services()
    initialize_payment_services()

# 获取配置信息
def get_service_status():
    """获取所有服务状态"""
    return {
        "ai_services": ai_service_manager.get_service_status(),
        "storage_services": {
            provider: {"available": True, "provider": provider}
            for provider in storage_manager.get_all_services().keys()
        },
        "payment_services": {
            provider: {
                "available": True, 
                "provider": provider,
                "supported_methods": service.get_supported_methods()
            }
            for provider, service in payment_manager.get_all_services().items()
        }
    }